package demo.user.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import demo.user.pojo.Menu;
import demo.user.pojo.User;
import demo.user.service.UserService;

@Controller
@RequestMapping("/user")
public class UserAction {

	@Autowired
	private UserService service;
	
	//判断用户名是否存在
	@RequestMapping("/checkUserName")
	public void checkUserName(HttpServletRequest request,HttpServletResponse response,String email) throws Exception{
		int num = service.getCountByName(email);
		System.out.println(num);
		String str = "";
		if(num==0){
			//返回不可用
			str = "<span style='color:red'>用户名不存在！</span>";
		}
		response.setContentType("html/text;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(str);
	}
	//用户登录
	@RequestMapping("/loginUser")
	public String loginUser(User user,HttpServletRequest request,HttpServletResponse response,Model model){
		String email = request.getParameter("email");
	    System.out.println(email);
		User u = service.findUserByEmail(email);
		System.out.println(u);
			if(!user.getPwd().equals(u.getPwd())){
				model.addAttribute("msg", "密码输入错误！");
				return "/login.jsp";
			}
			HttpSession session = request.getSession();
			session.setAttribute("user", u);
		return "/index.jsp";
	}
	//用户注册
	@RequestMapping("/registerUser")
	public String registerUser(User user,Model model,HttpServletRequest request){
		String ip = request.getRemoteAddr();
		user.setLastLoginIp(ip);
		user.setLastLoginTime(new Date());
		user.setLoginTimes(new Date());
		service.registerUser(user);
		model.addAttribute("user", user);
		return "/login.jsp";
	}
	//用户注销
	@RequestMapping("/exitUser")
	public String exitUser(HttpServletRequest request,HttpServletResponse response){
		//session初始化
		request.getSession().invalidate();
		return "/login.jsp";
	}
	//用户详情
	@RequestMapping("/userDetail")
	public String userDetail(Long uuid,Model model){
		User user = service.getUserDetailById(uuid);
		System.out.println(user);
		model.addAttribute("user", user);
		return "/jsp/user/userDetail.jsp";
	}
	//修改用户前操作
	@RequestMapping("/editUserBefore")
	public String editUserBefore(Long uuid,Model model){
		User user = service.getUserDetailById(uuid);
		model.addAttribute("user", user);
		return "/jsp/user/editUser.jsp";
	}
	//修改用户
	@RequestMapping("/editUser")
	public String editUser(User user){
		service.editUser(user);
		return "/user/userDetail.action?uuid="+user.getUuid();
	}
	//获得做列表树
	@RequestMapping("/getTblMenu")
	@ResponseBody
	public List<Menu> getTblMenu(Long uuid){
		return service.getTblMenu(uuid);
	}
}
